package br.unicentro.lynx.model.factory;

import java.sql.Connection;
import java.sql.SQLException;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class ReportFactory {
	private Connection connection;

	public JasperPrint getPrint(String url) {
		open();
		try {
			return JasperFillManager.fillReport(url, null, this.connection);
		} catch (JRException e) {
			throw new RuntimeException(e);
		} finally {
			close();
		}
	}

	private void open() {
		this.connection = ConnectionFactory.getConnection(
				ConnectionFactory.URL, ConnectionFactory.USER,
				ConnectionFactory.PASSWORD);
	}

	private void close() {
		try {
			this.connection.close();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

}
